<?php require_once 'header.php';?>
        <!-- Page Content -->
        <div id="page-wrapper">
            <div class="container-fluid">
                <div class="row">
                <div class="col-lg-12">
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            <?php echo XXF_BACK?>
                            <?php echo xdb::getInstance()->getvar("select content from todoprivate where id=$tdid")?> [附件]
                            <br>附件标题：<input type="text" id="title" value="附件标题">
                            <br><input type="file" id="file" style="display:inline;border:1px solid #000;" accept=".jpg" onchange="show()">
                            <img  src="" id="ysimg" width="400">
                            <br><input type="button" id="add" value="添加" onclick="add()">
                        </div>
                        <!-- /.panel-heading -->
                        <div class="panel-body">
                            <table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-example">
                                <thead>
                                    <tr>
                                        <th>序号</th>
                                        <th>标题</th>
                                        <th>内容</th>
                                        <th>时间</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                	<?php
                                	$i=0;
                                	foreach ($result as $row){$i++;
                                	?>
                                    <tr class="gradeA" id='<?php echo $row['id'];?>'>
                                    	<td><?php echo $i;?></td>
                                        <td><?php echo $row['title'];?></td>
                                        <td><img src="<?php echo str_replace("_", "+",$row['file'])?>" width="400"></td>
                                        <td><?php echo $row['atime'];?></td>
                                    	<td><a href='javascript:del(<?php echo $row['id']?>)' style='color:red;'>删</a> </td>
                                    </tr>
                                	<?php 
                                	}
                                	?>
                                </tbody>
                            </table>
                        </div>
                        <!-- /.panel-body -->
                    </div>
                    <!-- /.panel -->
                </div>
                <!-- /.col-lg-12 -->
            </div>
            <!-- /.row -->
             </div>
        </div>
        <!-- DataTables JavaScript -->
<?php require_once 'bottom.php';?>
    <script>

    var imgdata='';
    function add(){
        if(imgdata=='')return;
		$.xpost("todoprivate",{type:"addfujian",imgdata:imgdata.replace(/\+/g,"_"),tdsid:'<?php echo $tdsid?>',title:$("#title").val()},function(data){
			if(data){console.log(data);
				window.location.reload();
			}else{
				$.xloading(0);
				$.xalert("处理失败："+data);
			}
		});
    }
    function show(){
		$file=$("#file")[0].files[0];
		if($file.name.substr($file.name.length-4,4)=='.jpg'){
			$.xloading(1);
			var fr=new FileReader();
			fr.readAsDataURL($file);
			var img=new Image();
			fr.onload=function(e){
				if($file.size<100000){//100kb
					$("#ysimg").attr('src',fr.result);
	                $.xloading(0);
	                imgdata=fr.result;
	                return;
				}
				img.src=fr.result;
				img.onload=function(){
					var imageWidth = 0,    //压缩图片的大小  
                    imageHeight = 0,   
                    offsetX = 0,   
                    offsetY = 0,
                    realsize=1200;
					var canvas=document.createElement('canvas');
					var ctx=canvas.getContext('2d');
					if ((this.height > this.width)&&(this.height>realsize)) {
                        imageHeight = realsize;
                        imageWidth = Math.round(this.width * imageHeight / this.height);
                    } else if((this.height < this.width)&&(this.width>realsize)){
                        imageWidth = realsize;
                        imageHeight = Math.round(this.height * imageWidth / this.width);
                    }else{
                    	imageWidth = this.width;
                    	imageHeight=this.height;
                    }
					canvas.width=imageWidth;
					canvas.height=imageHeight;
                    ctx.drawImage(this, offsetX, offsetY, imageWidth, imageHeight);    
                    imgdata = canvas.toDataURL('image/jpeg',0.7);
                    $("#ysimg").attr('src',imgdata);
                    $.xloading(0);
				}
			}
		}else{
			$.xalert("附件请用jpg格式图片，可以取自手机照相。");
		}
    }
	function del(id){
		$.xconfirm("删除后无法恢复，确定要删除此条记录么？",function(data){
 			if(data){
				$.xloading(1);
				$.xtodb("del","todofujian",{},id,function(data){
					if(data==0){
						$.xloading(0);
						$.xalert("删除失败");
					}else{
						window.location.reload();
					}
				}); 
			} 
		});
	}
    </script>